<template>
  <a-modal
    title="编辑线上热门搜索"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="订单编号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入订单编号" v-decorator="['orderNumber', {rules: [{required: true, message: '请输入订单编号！'}]}]" />
        </a-form-item>
        <a-form-item
          label="订单状态"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select allow-clear placeholder="请选择终端类型" >
            <a-select-option v-for="(item,index) in tempInfo" :key="index" :value="item.id" @click="selectedValue(item.dictName)">{{ item.dictName }}</a-select-option>
          </a-select>
<!--          <a-input placeholder="请输入订单状态" v-decorator="['orderStatus', {rules: [{required: true, message: '请输入订单状态！'}]}]" />-->
        </a-form-item>
        <a-form-item
          label="投放时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入投放时间" v-decorator="['putTime', {rules: [{required: true, message: '请输入投放时间！'}]}]" />
        </a-form-item>
        <a-form-item
          label="终端数量"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入终端数量" v-decorator="['terminalNum', {rules: [{required: true, message: '请输入终端数量！'}]}]" />
        </a-form-item>
        <a-form-item
          label="城市"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入城市" v-decorator="['city', {rules: [{required: true, message: '请输入城市！'}]}]" />
        </a-form-item>
        <a-form-item
          label="播放时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入播放时间" v-decorator="['playTime', {rules: [{required: true, message: '请输入播放时间！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { pushSearchEdit } from '@/api/modular/system/pushSearchManage'
  import { getChildInfo } from '@/api/modular/system/fenDictManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        tempInfo: {},
        dictName: ''
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true
        getChildInfo({ 'dictCode': 'DDZT' }).then(res => {
          this.tempInfo = res.data
        })
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              orderNumber: record.orderNumber,
              orderStatus: record.orderStatus,
              putTime: record.putTime,
              terminalNum: record.terminalNum,
              city: record.city,
              playTime: record.playTime,
              status: record.status
            }
          )
        }, 100)
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            values['orderStatus'] = this.dictName
            pushSearchEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      selectedValue(dictName) {
        this.dictName = dictName
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
